#-*- coding: UTF-8 -*- 
import serial

import time         #延时使用
import binascii
import array
 
import threading


temp_h = 0  #温度高位
temp_l = 0  #温度低位
temp_p = 0  #温度小数点位


sound = "6 6 7 7 8 8 7 7 6 6 3 3 1 1 . . 5 5 4 4 3 3 4 5 4 0 4 4 5 5 6 6 7 7 5 5 2 2 4 4 3 3 2 3 4 3" #声音音调
sound_cnt = 0 #声音个数

sound_p = 6
s = serial.Serial("COM12", 115200)     #初始化串口
s2 = serial.Serial("COM1", 115200)     #初始化串口
def beep_play():
    global sound_cnt

    #print(sound[sound_cnt])  
    if(sound[sound_cnt]== ' '):
        s.write("p".encode())
    elif(sound[sound_cnt]== '.'):
        s.write(sound_p.to_bytes(1,'little'))
    else:
        s.write((int(sound[sound_cnt])+7).to_bytes(1,'little'))
    sound_cnt += 1
    #if(sound_cnt >=len(sound)):
        #sound_cnt = 0    
        #s.write(bytes.fromhex('71'))
        #s.write("s".encode())#字符用这个形式    
        #usart_state = 0   

def uart_send_IT():
    global usart_state,sound_cnt
    if(usart_state == 't'):
        print(temperature)
    else:
        s.write("T".encode())#字符用这个形式
        s.write(temp_h.to_bytes(1,'little'))
        s.write(temp_l.to_bytes(1,'little'))
        s.write(temp_p.to_bytes(1,'little'))
        s.write(bytes.fromhex('17'))#数字用这个形式
    
        t = threading.Timer(2,uart_send_IT)
        t.start()        
    #print("send")

data =0;#数据
str_cnt = 0 #字符位
data_cnt=3 #数据位
TEMP = 0#数据缓存
temperature= 0

usart_state = 0;

t = threading.Timer(2,uart_send_IT)
t.start()    
while True:
   
    #Hex_str = bytes.fromhex('10 11 12 34 3f')   #文本转换Hex
    ##=bytes.fromhex('10 11 12 34 3f') 
    #s.write(Hex_str)                            #串口发送 Hex_str()
    #接收
    n=s.inWaiting()                             #串口接收
    if n: 
        ##print("serial in")
        TEMP = s.read() #bytes类型数据
        if(TEMP == 't'.encode('utf-8')):#报时优先级最高
            
            print(temperature)            
            s.write("f".encode())#字符用这个形式
            usart_state == 't'
            while(sound_cnt <len(sound)):
                TEMP = s.read() #bytes类型数据                
                beep_play()
                time.sleep(0.1)
                
            t = threading.Timer(2,uart_send_IT)
            t.start()            
            sound_cnt = 0    
            s.write(bytes.fromhex('71'))
            usart_state = 0   
            #print("整点报时")                        
        #if(usart_state == 't'):
            #print("整点报时")
            #usart_state = 0;
        elif(usart_state == 'T'):
            if(data_cnt > 0):         #大端模式   
                data_cnt-=1
                data = TEMP[0]+data<<8*data_cnt
                ##print("data_remain",data_cnt,"data",data)
            else:
                data_cnt=3                
                usart_state = 0
                temperature=data*625/10000
                temp_p = int(temperature*10)%10
                temp_l = int(temperature)%10
                temp_h = int(temperature/10)%10
                #print(temperature) #计算出来的数值为十进制
                data = 0                
        #elif TEMP == 't'.encode('utf-8'):#时间信号
            ##print("t")
            #usart_state = 't'
        elif TEMP == 'T'.encode('utf-8'):#温度信号
            #print("T")
            usart_state = 'T'
        #else:
            #print("error data:",TEMP.hex())
    #time.sleep(0.0001)
